package trefliq.EJB.mdb;

import java.util.Date;
import javax.ejb.ActivationConfigProperty;
import javax.ejb.MessageDriven;
import javax.ejb.embeddable.EJBContainer;
import javax.jms.*;
import javax.naming.*;

/**
 *
 * @author Adam
 */
public class NewMovieSender {
    private Context _JndiContext;
    private ConnectionFactory _ConnectionFactory; 
    private Topic _NewMovieTopic; 
 
    private static EJBContainer _ec;

    public NewMovieSender() throws NamingException {
        //maybe need to move this to abstract class...
        _ec = EJBContainer.createEJBContainer();
        _JndiContext = _ec.getContext();
        
        //get the topic and stuff
        _ConnectionFactory = (ConnectionFactory) _JndiContext.lookup("jms/trefliq/ConnectionFactory");
        _NewMovieTopic = (Topic) _JndiContext.lookup("jms/trefliq/TopicNewMovieIn");   
 
    }

    public NewMovieSender(Context context) throws NamingException {
        //maybe need to move this to abstract class...
        _JndiContext = context;
        
        //get the topic and stuff
        _ConnectionFactory = (ConnectionFactory) _JndiContext.lookup("jms/trefliq/ConnectionFactory");
        _NewMovieTopic = (Topic) _JndiContext.lookup("jms/trefliq/TopicNewMovieIn");   
 
    }

    public boolean NotifyNewMovie(String movie) throws JMSException {
        // Connect to topic to add message
        Connection connection = _ConnectionFactory.createConnection();
        Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
        MessageProducer producer = session.createProducer(_NewMovieTopic);
        
        // Add topic message
        TextMessage message = session.createTextMessage();
        message.setText("Movie just in - " + movie);
        
        producer.send(message);
        // Clean up
        connection.close();
        return true;
    }
    
 }
